package it.free.maya.loadtest;

import it.free.maya.SearchEngineQueryProxy;
import it.free.maya.utilities.PropertiesLoader;

import java.util.Date;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * Testing entry point
 * @author Stefano Falconetti
 */
public class MayaTesting {

	public static long TESTS_EXECUTED = 0L;
	private static Logger logger = Logger.getLogger(MayaTesting.class);
	
	/**
	 * @param args are the configuration parameters, one in this case, the config file path
	 */
	public static void main(String[] args) {
		
		if( (args == null) || 
			(args.length != 1) ){
			logger.error("Wrong number of arguments, pass config file path only");
			System.exit(-1);
		}
		
		SearchEngineQueryProxy.getInstance();
		
		String configFilePath = null;
		
		Properties properties = null;
		
		String testquery = null;
		//numberofvirtualusers
		int numberOfVirtualUsers = 0;
		//rampupintervaltime
		long rampUpIntervalTime = 0L;
		//numberoftestspervirtualuser
		int numberOfTestsPerVirtualUser = 0;
		//intervalbetweentests
		long intervalBetweenTests = 0L;
		//verbose
		boolean verbose = false;
		
		Runnable worker = null;
		Thread runner = null;
		
		try{
			
			configFilePath = PropertiesLoader.getConfigFileFromClasspath(args[0]);
			logger.info("Reading test configuration from classpath file: [" + configFilePath + "]");
			
		    properties = PropertiesLoader.loadProperties(configFilePath);
		
		    //testquery
		    testquery = properties.getProperty("testquery");
			//numberofvirtualusers
			numberOfVirtualUsers = Integer.parseInt(properties.getProperty("numberofvirtualusers"));
			//rampupintervaltime
			rampUpIntervalTime = Long.parseLong(properties.getProperty("rampupintervaltime"));
			//numberoftestspervirtualuser
			numberOfTestsPerVirtualUser = Integer.parseInt(properties.getProperty("numberoftestspervirtualuser"));
			//intervalbetweentests
			intervalBetweenTests = Long.parseLong(properties.getProperty("intervalbetweentests"));
			//verbose
			verbose = Boolean.valueOf(properties.getProperty("verbose", "false")).booleanValue();
			
			logger.info("Configuration loaded, starting test: " + (new Date()).toString());
			
			logger.info("testquery=" + testquery);
			logger.info("numberofvirtualusers=" + numberOfVirtualUsers);
			logger.info("rampupintervaltime=" + rampUpIntervalTime);
			logger.info("numberoftestspervirtualuser=" + numberOfTestsPerVirtualUser);
			logger.info("intervalbetweentests=" + intervalBetweenTests);
			logger.info("verbose=" + verbose);
			
			
			for(int threadCounter = 0; threadCounter < numberOfVirtualUsers; threadCounter++){
			    
				worker = new VirtualUser(testquery, 
			    						 numberOfTestsPerVirtualUser, 
			    						 intervalBetweenTests,
			    						 verbose);
			    
			    runner = new Thread(worker, "maya.virtual.user." + threadCounter);
			    logger.info("Virtual user " + threadCounter + " starts");
			    runner.start();
			    Thread.sleep(rampUpIntervalTime);
			}
			
		}catch(Exception exc){
			   logger.error("Configuration loading error:", exc);
		}
	}
}
